<?php

namespace app\common\model\mysql;

use think\Model;

class SOrder extends Model
{
    /**
     * 查询拼团商品数据列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getNormalOrderList($where, $field,$limit,$page){
        $order = [
            "order_id" =>"desc",
        ];
        $result = $this->where($where)->field($field)->order($order)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
        return $result;
    }

    /**
     * 根据$id获取订单数据
     * @param $id
     * @return SysPost|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOrderByIdFind($id,$field = '*'){
        if(empty($id)){
            return false;
        }
        $where = [
            "order_id" =>$id,
        ];
        $result = $this->where($where)->field($field)->find();
        return $result;
    }

    public function DelOrderIds($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['order_id', 'in', $ids],
            ['del_flag', '=', '0']
        ];
        $data = [
            'del_flag'=>'2'
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }

    public function getOrderByDelFlagId($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['order_id', 'in', $ids],
            ['is_del', '=', '0']
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    public function getOrderByPayTypeId($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['order_id', 'in', $ids],
            ['pay_type', 'not in', [5,6]],
            ['paid','<>',0]
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    public function PayOrderIds($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['order_id', 'in', $ids],
            ['paid', '=', 0]
        ];
        $data = [
            'paid'=>1,
            'status'=>3,
            'pay_time'=>date("Y-m-d H:i:s",time()),
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }

    public function refundOrderIds($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['order_id', 'in', $ids],
            ['paid', '=', 1]
        ];
        $data = [
            'status'=>5,
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }

    public function receiveOrderIds($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['order_id', 'in', $ids],
            ['paid', '=', 1],
            ['status','=',1]
        ];
        $data = [
            'status'=>3,
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }

    /**
     * 订单总数
     * @param $where
     * @return int
     * @throws \think\db\exception\DbException
     */
    public function orderPayCount($where){
        $result = $this->where($where)->count();
        return $result;
    }
}
